<template>
  <SearchContainer is-width @search="queryList" @reset="() => formRef.resetFields()">
    <a-form ref="formRef" class="form-style-flex justify-between" :model="formState">
      <div class="flex">
        <a-form-item name="bqbh" label="" class="w-56">
          <SelectArea v-model:value="formState.bqbh" set-cache use-cache @select="queryList()" />
        </a-form-item>
        <a-form-item name="kssj" :label="switchis ? '下达时间' : '申请时间'" class="w-[300px] mx-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item v-if="formState.hbzj === '0'" name="bedCode" label="" class="w-56 mr-2">
          <a-input v-model:value="formState.bedCode" placeholder="床位号" allow-clear />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="w-56">
          <a-input v-model:value="formState.pyjm" :placeholder="formState.hbzj === '1' ? '药品名称/助记码' : '姓名/助记码/住院号'" allow-clear />
        </a-form-item>
        <a-form-item v-if="isShowSwitch" :label="isTablet ? '合并片剂' : '合并针剂'" name="delivery" class="w-[200px] mx-3">
          <a-switch v-model:checked="formState.hbzj" un-checked-value="0" checked-value="1" @change="onSwitech" />
        </a-form-item>
      </div>
      <div class="flex">
        <SelectDrugRoom
          v-model:value="formState.drugStoreId"
          placeholder="选择药房"
          select-first
          :params="{ isProducer: '1' }"
          class="w-52 mb-2 mr-4"
          @select="onSelectDrugRoom"
        />
        <a-form-item class="w-48" label="审核人">
          <a-select v-model:value="formState.shr" :options="pharmacistSelectData" allow-clear placeholder="请选择" @change="setLocalStorageEvent" />
        </a-form-item>
      </div>
    </a-form>
  </SearchContainer>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SelectArea from '@/components/SelectArea'
import useUserStore from '@/store/user'
import useStore from '@/store/area'

const areaStore = useStore()
const { empId } = useUserStore()
const formRef = ref()
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  hbzj: '1',
  bqbh: areaStore.areaCode,
  ward: ''
})
defineProps({
  switchis: {
    type: Boolean,
    default: false
  },
  isShowSwitch: {
    type: Boolean,
    default: true
  }
})

const expand = ref(false)
const pharmacistSelectData = ref([])
const emit = defineEmits(['query', 'swit', 'switchis'])

const onSwitech = () => {
  formState.value.bedCode = ''
  formState.value.pyjm = ''
  emit('swit', formState.value.hbzj)
}

const onSelectDrugRoom = (val) => {
  formState.value.drugStoreId = val.id
  window.localStorage.setItem(
    'dispensingDrugStoreId',
    JSON.stringify({
      value: formState.value.drugStoreId
    })
  )
  emit('query', formState)
}
//获取审核人下拉（类别为药剂师）
const getSelectPharmacist = async () => {
  const { code, data } = await post('/operation/opex/getEmpInfoList', {
    emplbid: '5',
    yxzt: 0,
    pageNum: 1,
    pageSize: 9999,
    notEmpIdList: [empId] // 当前登录但id
  })
  if (code !== 0) return
  pharmacistSelectData.value = data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
  const reviewerValue = JSON.parse(window.localStorage.getItem('dispensingReviewer'))
  if (reviewerValue && reviewerValue.time && reviewerValue.value) {
    const flag = pharmacistSelectData.value.some((item) => item.value === reviewerValue.value)
    //保存24小时
    if (dayjs(dayjs().format()).diff(reviewerValue.time, 'hour') >= 0 && dayjs(dayjs().format()).diff(reviewerValue.time, 'hour') < 24 && flag) {
      formState.value.shr = reviewerValue.value
    }
  }
}
const getDepartInfo = async () => {
  const res = await post('/common/masterData/getByYsbhDepartment', { empId: empId })
  if (res.code !== 0) message.error(res.msg || '获取失败')
  formState.value.drugStoreId = res.data?.defaultStoreId
}
const queryList = () => {
  expand.value = false
  if (!formState.value.drugStoreId) return
  emit('query', formState)
}
const setLocalStorageEvent = () => {
  window.localStorage.setItem(
    'dispensingReviewer',
    JSON.stringify({
      time: dayjs().format(),
      value: formState.value.shr
    })
  )
}
const isTablet = location.href.includes('tablet') > 0
onMounted(async () => {
  getSelectPharmacist()
  const drugStoreIdValue = JSON.parse(window.localStorage.getItem('dispensingDrugStoreId'))
  if (drugStoreIdValue && drugStoreIdValue.value) {
    formState.value.drugStoreId = drugStoreIdValue.value
  } else {
    await getDepartInfo()
  }
  queryList()
})
</script>
<style lang=""></style>
